package edu.uic.cs.i422.s2011.team4;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

public class SettingObservation extends Activity implements OnItemClickListener {
	
	enum State { YES, NO }	
	State[] stateOf;
	
	Toast mToastHandler;
	Integer[] mImages = {
            R.drawable.single_family, R.drawable.apartments,
            R.drawable.high_rise, R.drawable.residential_park,
            R.drawable.forest_park, R.drawable.botanical_gardens,
            R.drawable.comercial_establishments, R.drawable.school,
            R.drawable.vacant_lot, R.drawable.cemetery,
            R.drawable.farmland
    };
	Integer[] mStrings = {
    		R.string.settingItem1, R.string.settingItem2,
    		R.string.settingItem3, R.string.settingItem4,
    		R.string.settingItem5, R.string.settingItem6,
    		R.string.settingItem7, R.string.settingItem8,
    		R.string.settingItem9, R.string.settingItem10,
    		R.string.settingItem11,
    };
	Integer[] mStringsHelp = {
			R.string.settingItem1Help, R.string.settingItem2Help,
    		R.string.settingItem3Help, R.string.settingItem4Help,
    		R.string.settingItem5Help, R.string.settingItem6Help,
    		R.string.settingItem7Help, R.string.settingItem8Help,
    		R.string.settingItem9Help, R.string.settingItem10Help,
    		R.string.settingItem11Help,
    };
	
	/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);      
        /**
         * Set all to NO
         */        
        stateOf = new State[mImages.length];
        for (int i = 0; i < mImages.length; i++) {
        	stateOf[i] = State.NO;
        }
        
        /**
         * Get our UI Generator for this Activity and set the title, help, and tree
         * Note that the strings should be R.string....
         */
        SquirrelUIGenerator squirrel = new SquirrelUIGenerator(this);
        squirrel.setTitle(R.string.settingTitle);
        squirrel.setHelp(R.string.settingHelp);
        squirrel.setBreadCrumb(R.string.settingBreadCrumb);
        
        /**
         * Create the encapsulating layout for this activity. We are going to add
         * every View we create here using addView().
         */
        LinearLayout layout = new LinearLayout(this);
        layout.setOrientation(LinearLayout.VERTICAL); //Set Orientation
        layout.setScrollContainer(true);
        
        /**
         * Bring forth my setting template
         */
        LinearLayout setting = (LinearLayout) View.inflate(this, R.layout.setting, null);
        
        /**
         * Point to grid and add the images and all the necessary functionality
         */
        GridView grid = (GridView) setting.findViewById(R.id.settingGrid);
        grid.setAdapter(new ItemsAdapter(this, mImages, mStrings));
        grid.setOnItemClickListener(this);
        
        Button done = (Button) setting.findViewById(R.id.settingDone);
        done.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
              finish();
            }
        });
        
        /**
         * This is how you should create the header. The title should be already have
         * been passed above. The function just returns the View which is created by
         * the SquirrelUIGenerator.
         */
        layout.addView(squirrel.getHeader());
        layout.addView(setting);
        
        /**
         * Finally we put our whole View into this Activity Content View, so we can
         * show it. 
         */
        setContentView(layout);
        
        /**
         * Welcome Message
         */
        mToastHandler = Toast.makeText(this, R.string.settingHelp, Toast.LENGTH_LONG);
        mToastHandler.show();
    }

	@Override
	public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
		/* Notify the user */
		mToastHandler.cancel();
		mToastHandler.setDuration(Toast.LENGTH_SHORT);
		mToastHandler.setText(mStringsHelp[position]);
		mToastHandler.show();
		
		/* get the current views Image Indicator */
		ImageView indicator = (ImageView) v.findViewById(R.id.settingItemIndicator);
		
		/* Change Indicator */
		switch (stateOf[position]) {
			case YES :				
				stateOf[position] = State.NO;
				indicator.setImageResource(R.drawable.squirrel_no);
				break;
			case NO :
				stateOf[position] = State.YES;
				indicator.setImageResource(R.drawable.squirrel_yes);
				break;
		}
	}
}
